import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    saveEquipment, //保存设备
    getEquipmentById, //设备详情
    saveDetails, //设备参数保存
    getDetailsList, //设备参数详情
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow", //当前行
        "fCompanyid",
        "fCompanyname",
        "statusDialog"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            btnStatus: [],
            dialogHeight: null,
            formObj: [],
            formData: { //设备
                fId: "", //新增不传
                fProjectid: "",
                fProjectname: "",
                fProjectfullname: "",
                fCompanyid: "",
                fCompanyname: "",
                fCompanyfullname: "",
                serialNumber4g: "", //设备序列号(三轴)
                equipmentType: "", //设备类型(三轴)
                equipmentNumber: "", //设备识别号
                equipmentModel: "", //设备型号
                equipmentVer: "", //设备版本
                iccid4g: "" //物联网卡号
            },
            parameterData: { //设备参数
                fId: "", //
                fProjectid: "", //
                fProjectname: "", //
                fProjectfullname: "", //
                fCompanyid: "", //
                fCompanyname: "", //
                fCompanyfullname: "", //
                fEquipmentid: "", //设备Id
                fEquipmentnumber: "", //设备识别号
                fTestpattern: "", //测试模式
                fGunitemode: "", //喷浆模式
                fPilemachinemodel: "", //桩机模式
                fPilemodel: "", //桩模式
                fStationmode: "", //桩号模式
                fPiledrivernumber: "", //桩机编号
                fMileagenumber: "", //里程号
                fPilenoa: "", //桩排号A
                fPilenob: "", //桩排号B
                fCurrentstationa: "", //当前桩号A
                fCurrentstationb: "", //当前桩号B
                fDesignpilelength: "", //设计桩长
                fPiledistance: "", //桩间距
                fCementdensity: "", //水泥密度
                fWatercementratio: "", //水灰比
                fDeviseashcontent: "", //设计段灰量
                fAirstirringdepth: "", //空搅深度
                fDrillingspeed: "", //下钻速度
                fLiftingspeed: "", //提钻速度
                fRoundtripnumber: "", //往返次数
                fDevisepilediameter: "", //设计桩径
                fExpandpilediameter: "", //扩大头桩径
                fExpandpilelength: "", //扩大头桩长
                fExpandashcontent: "", //扩大头灰量
                fDesignverticality: "", //设计垂直度
                fPerpendicularitypd: "", //设计垂直度正偏差
                fPilelengthmd: "", //设计桩长负偏差
                fAshcontentmd: "", //设计总灰量负偏差
                fDrillingspeedpd: "", //设计下钻速度正偏差
                fLiftingspeedpd: "", //设计提钻速度正偏差
                fForewarning: true, //屏上预警开关(0,1)
                fEmptypilemixing: true, //空桩是否搅拌(0,1)
                fAirchurncalculation: true, //空搅是否计算(0,1)
                fLocationtype: "", //定位类型
                fLongitude: "", //经度
                fLatitude: "", //纬度
                fAshcalculation: "", //灰量计算
                fSlurrydensity: "", //浆液密度
                fTypeofmachinery: "", //机械类型
                fGearspeed: "", //档转速(0,1,2)


                fCurrentlimiting: "" //电流限值
            },
            btnShow: true,
            currentDetail: null,
            fStatus1: 1,
            fStatus2: 1,
            fStatus3: 1,

            fCompanynames: null,
            fCompanyids: null,
            iSJpSieveListBox: 0,
            tableData: []
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    if (newVal == 1 || newVal == 2) { //设备
                        this.formObj = formJs.formEquipmentObj
                        // this.formData.fDetecttype = "自检"
                    } else if (newVal == 4) { //设备参数
                        this.formObj = formJs.formParameterNoObj
                        // this.formData.fDetecttype = ""

                    }
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    this.formData = this.$options.data.call(this).formData;

                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: newVal.fId,
                        });
                    }
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanyids = newVal

                }
            },
            immediate: true,
        },
        fCompanyname: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal

                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        // if (this.statusDialog == 0) { //设备

        // } else if (this.statusDialog == 1) { //设备参数

        // }
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        this.btnShow = true
        if (this.statusNum == 1) { //新增
            this.formObj = formJs.formEquipmentObj
            this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.formObj.map((item) => {
                item.disabled = false;
            });
        } else if (this.statusNum == 2) { //编辑
            this.formObj = formJs.formEquipmentObj
            this.formData.fId = this.currentDetail.fId //按钮 
            this.formObj.map((item) => {
                item.disabled = false;
            });
        } else if (this.statusNum == 4) { //设备参数设置
            this.formObj = formJs.formParameterNoObj
            this.parameterData.fEquipmentid = this.currentDetail.fId //按钮 
            this.parameterData.fEquipmentnumber = this.currentDetail.equipmentNumber
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;


        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            if (this.statusDialog == 0) {
                this.formObj = formJs.formEquipmentObj

                this.formObj.map((item) => {
                    item.disabled = true;
                });
            } else if (this.statusDialog == 1) {
                // 变化
                this.formObj = formJs.formParameterNoObj
                this.formObj.map((item) => {
                    item.disabled = true;
                });
            }

        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            if (this.statusDialog == 0) {
                this.dialogHeight = h * 0.3 + "px";
            } else if (this.statusDialog == 1) {
                this.dialogHeight = h * 0.76 + "px";
            }
        },
        closeDialog() {
            this.$emit("close");
        },
        formSwitchChange(val, index, prop) {
            if (prop == 'fForewarning') {
                if (val == false) {
                    this.fStatus1 = 0
                } else if (val == true) {
                    this.fStatus1 = 1
                }
            } else if (prop == 'fEmptypilemixing') {
                if (val == false) {
                    this.fStatus2 = 0
                } else if (val == true) {
                    this.fStatus2 = 1
                }
            } else if (prop == 'fAirchurncalculation') {
                if (val == false) {
                    this.fStatus3 = 0
                } else if (val == true) {
                    this.fStatus3 = 1
                }
            }
        },
        selectChange(value, index, prop) {
            if (prop == 'fPilemachinemodel') {
                if (value == '双轴') {


                    if (this.parameterData.fPilemodel == '' || this.parameterData.fPilemodel == '普通') {
                        this.formObj = formJs.formParameterBiaxObj
                    } else if (this.parameterData.fPilemodel == '钉型') { //都选
                        this.formObj = formJs.formParameterObj

                    }
                } else if (value == '单轴') {
                    if (this.parameterData.fPilemodel == '' || this.parameterData.fPilemodel == '普通') {
                        this.formObj = formJs.formParameterNoObj
                    } else if (this.parameterData.fPilemodel == '钉型') { //只选钉型
                        this.formObj = formJs.formParameterPileModeObj

                    }
                }

            } else if (prop == 'fPilemodel') {
                if (value == '钉型') {


                    if (this.parameterData.fPilemachinemodel == '' || this.parameterData.fPilemachinemodel == '单轴') {
                        this.formObj = formJs.formParameterPileModeObj

                    } else if (this.parameterData.fPilemachinemodel == '双轴') {
                        this.formObj = formJs.formParameterObj

                    }
                } else if (value == '普通') {
                    if (this.parameterData.fPilemachinemodel == '' || this.parameterData.fPilemachinemodel == '单轴') {
                        this.formObj = formJs.formParameterNoObj

                    } else if (this.parameterData.fPilemachinemodel == '双轴') {
                        this.formObj = formJs.formParameterBiaxObj

                    }
                }
            }

            // fPilemachinemodel
        },

        //   edit(status)
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        JpSieveListFun(data, i) {
            this.iSJpSieveListBox = i;
            this.parameterData = data
            if (data.fForewarning == 0) { //屏上预警开关(0,1)
                this.parameterData.fForewarning = false
            } else if (data.fForewarning == 1) {
                this.parameterData.fForewarning = true
            }
            this.getFormType(data)


        },
        // 详情表类型
        getFormType(data) {

            if (data.fGearspeed == "0") {
                this.parameterData.fProjectfullname = "Ⅰ"
            } else if (data.fGearspeed == "1") {
                this.parameterData.fProjectfullname = "Ⅱ"
            } else if (data.fGearspeed == "2") {
                this.parameterData.fProjectfullname = "Ⅲ"
            }
            if (data.fEmptypilemixing == 0) { //空桩是否搅拌(0,1)
                this.parameterData.fEmptypilemixing = false
            } else if (data.fEmptypilemixing == 1) {
                this.parameterData.fEmptypilemixing = true
            }
            if (data.fAirchurncalculation == 0) { //空搅是否计算(0,1)
                this.parameterData.fAirchurncalculation = false
            } else if (data.fAirchurncalculation == 1) {
                this.parameterData.fAirchurncalculation = true
            }
            if (data.fPilemachinemodel == '单轴' && data.fPilemodel == '普通') {
                //桩模式普通 && 桩机模式单轴 都不选
                this.formObj = formJs.formParameterNoObj;
            } else if (data.fPilemachinemodel == '双轴' && data.fPilemodel == '普通') {
                // //桩模式普通 && 桩机模式双轴 先选选择双轴
                this.formObj = formJs.formParameterBiaxObj;
            } else if (data.fPilemachinemodel == '单轴' && data.fPilemodel == '钉型') {
                //桩模式钉型 && 桩机模式单轴 钉型模式
                this.formObj = formJs.formParameterPileModeObj;
            } else if (data.fPilemachinemodel == '双轴' && data.fPilemodel == '钉型') {
                // 桩模式钉型 && 桩机模式双轴 都选
                this.formObj = formJs.formParameterObj;

            }
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            var url;
            if (this.statusDialog == 0) {
                url = getEquipmentById
            } else if (this.statusDialog == 1) {
                url = getDetailsList
            }
            url(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;

                    var data = res.data;
                    if (this.statusDialog == 0) {
                        this.formData = data.data
                    } else if (this.statusDialog == 1) {
                        this.tableData = data;
                        if (data.length != 0) {
                            this.parameterData = data[0]
                            this.getFormType(data[0])
                        }




                    }


                    if (this.statusNum == 7) {
                        this.btnShow = false;
                        this.formObj.map((item) => {
                            item.disabled = true;
                        });
                    } else {
                        this.btnShow = true;
                    }


                } else {
                    this.$message.error("列表数据获取失败", res.msg);
                    this.loading = false;
                }
            });
        },

        yj_saveForm() {
            if (this.statusDialog == 0) {
                this.formData.fProjectid = this.userInfo.projectId; //当前登录人id
                this.formData.fProjectname = this.userInfo.projectName; //当前登录人名称
                this.formData.fProjectfullname = this.userInfo.projectName; //当前登录人名称
                this.formData.fCompanyid = this.fCompanyids; //选中标段id
                this.formData.fCompanyname = this.fCompanynames; //选中标段名称
                this.formData.fCompanyfullname = this.fCompanynames; //选中标段名称
                if (this.$refs.publicForm.submitForm()) {

                    this.saveForm(this.formData);

                } else {
                    console.log("error");
                }
            } else if (this.statusDialog == 1) {
                this.parameterData.fProjectid = this.userInfo.projectId; //当前登录人id
                this.parameterData.fProjectname = this.userInfo.projectName; //当前登录人名称
                // this.parameterData.fProjectfullname = this.userInfo.projectName; //当前登录人名称
                this.parameterData.fCompanyid = this.fCompanyids; //选中标段id
                this.parameterData.fCompanyname = this.fCompanynames; //选中标段名称
                this.parameterData.fCompanyfullname = this.fCompanynames; //选中标段名称

                if (this.parameterData.fProjectfullname == "Ⅰ") {
                    this.parameterData.fGearspeed = 0
                } else if (this.parameterData.fProjectfullname == "Ⅱ") {
                    this.parameterData.fGearspeed = 1
                } else if (this.parameterData.fProjectfullname == "Ⅲ") {
                    this.parameterData.fGearspeed = 2
                }
                if (this.$refs.publicForm1.submitForm()) {
                    this.parameterData.fForewarning = this.fStatus1
                    this.parameterData.fEmptypilemixing = this.fStatus2
                    this.parameterData.fAirchurncalculation = this.fStatus3
                    this.saveForm(this.parameterData);
                } else {
                    console.log("error");
                }
            }

        },
        // 保存
        saveForm(data) {
            this.loading = true;
            var url;
            if (this.statusDialog == 0) {
                url = saveEquipment
            } else if (this.statusDialog == 1) {
                url = saveDetails
            }
            this.isLock = true
            // this.formData.fStatus = this.fStatus == true ? 1 : 0
            url(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.data);
                        console.log("接口报错");
                    }
                })

        },
    },
};